{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 20180106解析json数据的几种方法\n",
    "* blog url: [CSDN Blog](http://blog.csdn.net/chengcheng95588/article/details/78991511)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1. 内置json模块 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.1 示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "解析后的json数据:\n",
      " {'errorNo': 0, 'errorMsg': 'SUCCESS', 'latestTimelineStamp': '20180105121318000', 'preClose': 33.459999084473, 'tick': [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]}\n",
      "取json数据：\n",
      " [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "str1 = 'jQuery180({\"errorNo\": 0, \"errorMsg\": \"SUCCESS\", \"latestTimelineStamp\": \"20180105121318000\", \"preClose\": 33.459999084473, \"tick\": [{\"date\": 20180105, \"time\": 113002000, \"price\": 33.990001678467, \"volume\": 37100, \"bsflag\": \"B\", \"ccl\": 0}]});'\n",
    "# 去除两边的无用数据\n",
    "json_str1 = str1.strip(\"jQuery180();\")\n",
    "# 解析json字符串\n",
    "json_data1 = json.loads(json_str1)\n",
    "print(\"解析后的json数据:\\n\", json_data1)\n",
    "print(\"取json数据：\\n\", json_data1['tick'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2 示例\n",
    "* 错误示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import json\n",
    "# str1 = \"{'errorNo': 0, 'errorMsg': 'SUCCESS', 'latestTimelineStamp': '20180105121318000', 'preClose': 33.459999084473, 'tick': [{'date': 20180105}]}\"\n",
    "# # 解析json字符串\n",
    "# json_data1 = json.loads(str1)\n",
    "# print(\"解析后的json数据:\\n\", json_data1)\n",
    "# print(\"取json数据：\\n\", json_data1['tick'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.1 示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "解析后的json数据:\n",
      " {'errorNo': 0, 'errorMsg': 'SUCCESS', 'latestTimelineStamp': '20180105121318000', 'preClose': 33.459999084473, 'tick': [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]}\n",
      "取json数据：\n",
      " [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]\n"
     ]
    }
   ],
   "source": [
    "import demjson\n",
    "str1 = \"{'errorNo': 0, 'errorMsg': 'SUCCESS', 'latestTimelineStamp': '20180105121318000', 'preClose': 33.459999084473, 'tick': [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]}\"\n",
    "json_data1 = demjson.decode(str1)\n",
    "print(\"解析后的json数据:\\n\", json_data1)\n",
    "print(\"取json数据：\\n\", json_data1['tick'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2 示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "解析后的json数据:\n",
      " {'errorNo': 0, 'errorMsg': 'SUCCESS', 'latestTimelineStamp': '20180105121318000', 'preClose': 33.459999084473, 'tick': [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]}\n",
      "取json数据：\n",
      " [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]\n"
     ]
    }
   ],
   "source": [
    "import demjson\n",
    "str1 = '''{'errorNo': 0, errorMsg: 'SUCCESS', \"latestTimelineStamp\": '20180105121318000', 'preClose': 33.459999084473, 'tick': [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]}'''\n",
    "json_data1 = demjson.decode(str1)\n",
    "print(\"解析后的json数据:\\n\", json_data1)\n",
    "print(\"取json数据：\\n\", json_data1['tick'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.3 示例\n",
    "* 错误示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import demjson\n",
    "# str1 = '''{errorNo': 0, errorMsg: 'SUCCESS', \"latestTimelineStamp\": '20180105121318000', 'preClose': 33.459999084473, 'tick': [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]}'''\n",
    "# json_data1 = demjson.decode(str1)\n",
    "# print(\"解析后的json数据:\\n\", json_data1)\n",
    "# print(\"取json数据：\\n\", json_data1['tick'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.1 示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "解析后的json数据:\n",
      " {'errorNo': 0, 'errorMsg': 'SUCCESS', 'latestTimelineStamp': '20180105121318000', 'preClose': 33.459999084473, 'tick': [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]}\n",
      "取json数据：\n",
      " [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]\n"
     ]
    }
   ],
   "source": [
    "import jsonlike\n",
    "str1 = '''{'errorNo': 0, errorMsg: 'SUCCESS', \"latestTimelineStamp\": '20180105121318000', 'preClose': 33.459999084473, 'tick': [{'date': 20180105, 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]}'''\n",
    "json_data1 = jsonlike.loads(str1)\n",
    "print(\"解析后的json数据:\\n\", json_data1)\n",
    "print(\"取json数据：\\n\", json_data1['tick'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.2 示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "解析后的json数据:\n",
      " {'error': 0, 'errorMsg': 'SUCCESS', 'uptime': '20180105121318000', 'preClose': 33.459999084473, 'tick': [{'date': '20180105', 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]}\n",
      "取json数据：\n",
      " [{'date': '20180105', 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': 'B', 'ccl': 0}]\n"
     ]
    }
   ],
   "source": [
    "import jsonlike\n",
    "str1 = '''var hei ha {'error': 0, 'errorMsg': 'SUCCESS', uptime: '20180105121318000', 'preClose': 33.459999084473, tick: [{'date': '20180105', 'time': 113002000, 'price': 33.990001678467, 'volume': 37100, 'bsflag': \"B\", \"ccl\": 0}]};'''\n",
    "json_data1 = jsonlike.unwrap_and_load(str1)\n",
    "print(\"解析后的json数据:\\n\", json_data1)\n",
    "print(\"取json数据：\\n\", json_data1['tick'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
